package com.main.toyexchange;

/*
 * 主页面，显示所有的玩具信息
 * 可以根据关键字搜索玩具
 */

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bjtu.toyexchange.R;
import com.item.toyexchange.ToyInfoActivity;
import com.util.toyexchange.DialogUtil;
import com.util.toyexchange.HttpUtil;

public class IndexActivity extends Activity{
	private ListView listview;
	private LayoutInflater layout;
	private String username;
	private JSONArray jsonArr;
	private EditText searchText = null;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        
        Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		
		username = bundle.getString("username");
		
		//搜索关键字的edittext
		searchText = (EditText)findViewById(R.id.edit_search);
		
		indexPro();
        findViewById();
		initView();
	}
	
	protected void findViewById() {
		listview = (ListView)this.findViewById(R.id.toyList);

		listview.setAdapter(new CatergorAdapter());
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterview, View view, int parent,
					long id) {
				Intent intent = new Intent();
				String toy_title = mTitleValues[parent];
				String toy_content = mContentValues[parent];
				String toy_level = mlevel[parent];
				String toy_state = mstate[parent];
				String toy_user = muser[parent];
				String toy_publishdate = mpublishdate[parent];
				String toy_photo = mPhoto[parent];
				Integer toy_id = mId[parent];
				
				intent.putExtra("title", toy_title);
				intent.putExtra("content", toy_content);
				intent.putExtra("level", toy_level);
				intent.putExtra("state", toy_state);
				intent.putExtra("tphoto", toy_photo);
				intent.putExtra("tuser", toy_user);
				intent.putExtra("tid", toy_id);
				intent.putExtra("date", toy_publishdate);
				intent.putExtra("user", username);
				intent.setClass(IndexActivity.this, ToyInfoActivity.class);
				IndexActivity.this.startActivity(intent);
			}
		});
	}

	protected void initView() {

	}
	
	private class CatergorAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return jsonArr.length();
		}

		@Override
		public Object getItem(int position) {
			return jsonArr.optJSONObject(position);
		}

		@Override
		public long getItemId(int position) {
			try {
				return ((JSONObject)getItem(position)).getInt("t_id");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return 0;
		}

		@SuppressWarnings("null")
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder holder = new ViewHolder();
			layout = LayoutInflater.from(IndexActivity.this);
			
			//组装数据
			if(convertView==null){
				convertView = layout.inflate(R.layout.toy_item, null);
				holder.image = (ImageView)convertView.findViewById(R.id.toy_pic);
				holder.title = (TextView)convertView.findViewById(R.id.toy_title);
				holder.content = (TextView)convertView.findViewById(R.id.toy_info);
				//使用tag存储数据
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder)convertView.getTag();
			}
			
				holder.image.setImageResource(mImageIds[position]);
				holder.title.setText(mTitleValues[position]);
				holder.content.setText(mContentValues[position]);
			return convertView;
		}
	}
	
	
	
	// 适配显示的图片数组
	private Integer[] mImageIds = null;
//		{R.drawable.pic,R.drawable.pic,R.drawable.pic,R.drawable.pic,
//			R.drawable.pic,R.drawable.pic,R.drawable.pic,R.drawable.pic
//			 };
	//玩具图片
	private String[] mPhoto = null;
	//玩具id
	private Integer[] mId = null;
	private String[] mTitleValues = null;
	private String[] mContentValues = null;
	private String[] mlevel = null;
	private String[] mstate = null;
	private String[] muser = null;
	private String[] mpublishdate = null;
	
	 public static class ViewHolder {
			ImageView image;
			TextView title;
			TextView content;
	 }
	 
	 private boolean indexPro(){
		    String url = HttpUtil.BASE_URL + "ToyListServlet";
			try
			{
				jsonArr = new JSONArray(HttpUtil.getRequest(url)); 
//				Log.v("lllllllll",jsonArr.toString());
				if (jsonArr!=null)
				{
					mTitleValues = new String[jsonArr.length()];
					mContentValues = new String[jsonArr.length()];
					mId = new Integer[jsonArr.length()];
					mPhoto = new String[jsonArr.length()];
					mImageIds = new Integer[jsonArr.length()];
					mlevel = new String[jsonArr.length()];
					mstate = new String[jsonArr.length()];
					muser = new String[jsonArr.length()];
					mpublishdate = new String[jsonArr.length()];
					for(int i=0;i < jsonArr.length();i++){
						JSONObject jsonObject = (JSONObject)jsonArr.opt(i);

						mTitleValues[i] = jsonObject.getString("t_name");
						mContentValues[i] = jsonObject.getString("t_description");
//						Log.v("tdescription", mContentValues[i].toString());
						mId[i] = jsonObject.getInt("t_id");
//						Log.v("tId", mId[i].toString());
						mPhoto[i] = jsonObject.getString("t_photo");
						mlevel[i] = jsonObject.getString("t_level");
						mstate[i] = jsonObject.getString("t_state");
						muser[i] = jsonObject.getString("u_name");
						mpublishdate[i] = jsonObject.getString("t_publishdate");
						
						mImageIds[i] = R.drawable.pic;
					}
					return true;
				}
				else
					DialogUtil.showDialog(this
							, "玩具列表更新失败或玩具列表为空！", false);
			}
			catch (Exception e)
			{
				DialogUtil.showDialog(this
					, "服务器响应异常，请稍后再试！", false);
				e.printStackTrace();
			}
			Log.v("2222", "ok");
			return false;
		}
}
